package com.aier.school.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.entity.SchoolGrade;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.entity.SchoolTeacherDuty;
import com.aier.school.service.ISchoolDutyService;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;

@Scope("prototype")
@Component("schoolDutyManageAction")
public class SchoolDutyManageAction extends SchoolBaseAction {

    /**
     * 学校管理-管理员查看的值班 与 教师查看的值班管理
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name = "schoolService")
    private ISchoolService schoolService;    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;    
    @Resource(name = "schoolDutyService")
    private ISchoolDutyService schoolDutyService;
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息

    private List<SchoolTeacherDuty> stDutys;    //教师值班表
    private List<String> tnameAccountList;        //教师名字账号列表
    private List<SchoolGrade> schoolGradeList;    //职务列表    
    private List<SchoolTeacher> schoolTeacherList;        //学校教师列表    
    
    private Integer sid;        //学校id
    private Long tid;            //教师id
    private String txt_sdate;    //开始时间
    private String txt_edate;    //结束时间
    private String account;        //教师账号
    private Long gid;            //职务id
    private Long did;            //值班id
    
    private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数

    @Override
    public String execute(){
        tnameAccountList = schoolTeacherService.searchTNameAndAccountBySid(sid);
        tnameAccountList.add(0, "请选择");
        schoolGradeList = schoolService.searchSchoolGradeList(sid);
        
        return SUCCESS;
    }
    
    //查询值班表 管理员
    @SuppressWarnings("unchecked")
    public String search_admin(){
        s = 0;
        if(page<=0){
            page=1;
        }
        List<Long> tids = new ArrayList<Long>();    
        List<SchoolTeacher> teachers = schoolTeacherService.selectTeacher(sid,account,gid);
        if(teachers!=null&&teachers.size()!=0){
            for(SchoolTeacher teacher:teachers){
                tids.add(teacher.getTid());
            }
            Map<String,Object> map = schoolDutyService.searchstDutysByCondition(page,SchoolFinalPageSize.SCHOOL_DUTY_LIST_PAGESIZE,
                    sid,tids,txt_sdate,txt_edate);
            if(null!=map){
                stDutys = (List<SchoolTeacherDuty>) map.get(ICommonDAO.OBJ_LIST);
                if (null != stDutys) {
                    s = 1;
                }
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);        
            }
        }
        return SUCCESS;
    }
    
  //查询值班表 普通教师
    @SuppressWarnings("unchecked")
    public String search_normal(){
        s = 0;
        if(page<=0){
            page=1;
        }
        List<Long> tids = new ArrayList<Long>();    
        tids.add(tid);
        
        Map<String,Object> map = schoolDutyService.searchstDutysByCondition(page,SchoolFinalPageSize.SCHOOL_DUTY_LIST_PAGESIZE,
                sid,tids,txt_sdate,txt_edate);
        if(null!=map){
            stDutys = (List<SchoolTeacherDuty>) map.get(ICommonDAO.OBJ_LIST);
            if (null != stDutys) {
                s = 1;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);        
        }
        return SUCCESS;
    }
    
    /**
     *  添加值班信息
     * @return
     */
    public String add() {
        s = 1;
        try {
            Timestamp sdate = Timestamp.valueOf(txt_sdate);
            Timestamp edate = Timestamp.valueOf(txt_edate);
            List<SchoolTeacher> teachers = schoolTeacherService.selectTeacher(sid,account,gid);
            if(teachers!=null&&teachers.size()!=0){
                for(SchoolTeacher teacher:teachers){
                    s = schoolDutyService.insertSchoolTeacherDuty(sid, teacher.getTid(),sdate, edate);
                } 
            }
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }
        return SUCCESS;
    }

    /**
     *  删除值班信息
     * @return
     */
    public String delete() {
        s = schoolDutyService.deleteSchoolTeacherDuty(did);
        return SUCCESS;
    }

    /**
     * 选择职务
     * @return
     */
    public String changetype(){
        if(gid==-1){
            tnameAccountList = schoolTeacherService.searchTNameAndAccountBySid(sid);
        }else{
            tnameAccountList = schoolTeacherService.searchTNameAndAccountBySidGid(gid,sid);          
        }

        return SUCCESS;
    }  
    
    
    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<SchoolTeacherDuty> getStDutys() {
        return stDutys;
    }

    public List<String> getTnameAccountList() {
        return tnameAccountList;
    }

    public List<SchoolGrade> getSchoolGradeList() {
        return schoolGradeList;
    }

    public List<SchoolTeacher> getSchoolTeacherList() {
        return schoolTeacherList;
    }

    public int getCount() {
        return count;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setSchoolDutyService(ISchoolDutyService schoolDutyService) {
        this.schoolDutyService = schoolDutyService;
    }

    public void setTxt_sdate(String txt_sdate) {
        this.txt_sdate = txt_sdate;
    }

    public void setTxt_edate(String txt_edate) {
        this.txt_edate = txt_edate;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public void setDid(Long did) {
        this.did = did;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    
    
}

